<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
      <!-- <el-breadcrumb-item>活动详情</el-breadcrumb-item> -->
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="showAddCateDialog"
            >添加分类</el-button
          >
        </el-col>
      </el-row>
      <!-- 表格 -->
      <tree-table
        class="treeTable"
        :selection-type="false"
        :data="cateList"
        :columns="columns"
        :expand-type="false"
        show-index
        index-text="#"
      >
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted == false"></i>
          <i class="el-icon-error" v-else></i>
        </template>
        <template slot="order" slot-scope="scope">
          <el-tag type="success" size="mini" v-if="scope.row.cat_level == 0"
            >一级</el-tag
          >
          <el-tag
            type="warning"
            size="mini"
            v-else-if="scope.row.cat_level == 1"
            >二级</el-tag
          >
          <el-tag type="info" size="mini" v-else>三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="opt" slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit"
            >编辑</el-button
          ><el-button type="danger" size="mini" icon="el-icon-delte"
            >删除</el-button
          >
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 3, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加分类的对话框 -->
    <el-dialog
      @close="addCateDialogVsible"
      title="提示"
      :visible.sync="addCatedialogVisible"
      width="30%"
    >
      <!-- 添加分类的表单 -->
      <el-form
        ref="addCateFormRef"
        :rules="addCateFormRules"
        :model="addCateForm"
        label-width="80px"
      >
        <el-form-item prop="cat_name" label="分类名称">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
            v-model="seletedKeys"
            :options="parentCateList"
            :props="cascaderProps"
            @change="parentCateChange"
            clearable
          ></el-cascader>
          <!-- <el-input v-model="addCateForm.cat_name"></el-input> -->
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 为ttable定义列
      columns: [
        {
          labe: "分类名称",
          prop: "cat_name",
        },
        {
          labe: "是否有效",
          type: "template",
          template: "isok",
        },
        {
          labe: "排序",
          type: "template",
          template: "order",
        },
        {
          labe: "操作",
          type: "template",
          template: "opt",
        },
      ],
      // 总数
      total: 1,
      // 商品分类列表
      cateList: [],
      //   查询条件
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      addCatedialogVisible: false,
      // 添加分类的数据
      addCateForm: {
        // 父亲id
        cat_pid: 0,
        cat_name: "",
        // 默认分类登记
        cat_level: 0,
      },
      // 添加表单的验证规则
      addCateFormRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
      },
      // 父级分类列表
      parentCateList: [],
      cascaderProps: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
        checkStrictly: true,
      },
      // 选中的父级分类id数组
      seletedKeys: [],
      
    };
  },
  created() {
    this.getCateList();
  },
  methods: {
    // 获取商品分类
    async getCateList() {
      const { data: res } = await this.$http.get("categories", {
        params: this.queryInfo,
      });
      // console.log(res);
      this.cateList = res.data.result;
      this.total = res.data.total;
    },
    handleCurrentChange(val) {
      // console.log(val);
      this.queryInfo.pagenum = val;
      this.getCateList();
    },
    handleSizeChange(val) {
      this.queryInfo.pagesize = val;
      this.getCateList();
    },
    // 点击按钮打开添加对话框
    showAddCateDialog() {
      // 先获取父级分类的数据列表
      this.getParentCateList();
      this.addCatedialogVisible = true;
    },
    // 获取父级分类的数据列表
    async getParentCateList() {
      const { data: res } = await this.$http.get("categories", {
        params: {
          // 2表示获取前两级分类
          type: 2,
        },
      });
      this.parentCateList = res.data;
      console.log(res);
    },
    // 选择项发生变化，触发
    parentCateChange() {
      console.log(this.seletedKeys);
      // 如果有选择分类就说明不是一级分类
      // 反之就是一级分类
      if (this.seletedKeys.length > 0) {
        this.addCateForm.cat_pid =
          this.seletedKeys[this.seletedKeys.length - 1];
        // 为当前分类的等级赋值 正好相等
        this.addCateForm.cat_level = this.seletedKeys.length;
        return;
      } else {
        this.addCateForm.cat_pid = 0;
        // 为当前分类的等级赋值 正好相等
        this.addCateForm.cat_level = 0;
      }
    },
    // 监听对话框的关闭事件，重置表单
    addCateDialogVsible() {
      this.$refs.addCateFormRef.resetFields();
      this.seletedKeys = [];
      this.addCateForm.cat_pid = 0;
      this.addCateForm.cat_level = 0;
    },
    // 添加表单
    addCate() {
      this.$refs.addCateFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post(
          "categories",
          this.addCateForm
        );
        if (res.meta.status != 201) {
          return this.$message.error("添加新的分类失败");
        }
        // this.addCateDialogVsible = false;
        
this.addCatedialogVisible=false
        this.getCateList();
        this.$message.success("添加分类成功");

      });
    },
  },
};
</script>

<style lang="less" scoped>
.treeTable {
  margin-top: 15px;
}
.el-cascader {
  width: 100%;
}
</style>>
